<?php

namespace App\Console\Commands;

use App\Customer;
use App\Jobs\Send;
use App\Lead;
use App\Project;
use App\ProjectUser;
use App\Record;
use App\User;
use App\UserProject;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;

class HengdongCommnad33 extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'guangte {number}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '广特总部大楼';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        $number = $this->argument('number');

        $leads = Lead::where('is_send','!=',1)
            ->where('create_time','>=',Carbon::parse("2022-01-01 00:00:00")->timestamp)
            ->take($number)
            ->get();
        /*    $customers = Customer::where('project_id', 39)
                ->orderBy('customer_id', 'asc')
                ->where('owner_user_id', '!=', $user_id)
                ->where('create_time', '>=', 1609430400)
                ->where('create_time', '<=', 1640966400)
                ->take($num)
                ->get();*/

        foreach ($leads as $item) {
            /**
             * @var  Lead $item
             */
            if (!$item->customer_id){
                continue;
            }
            $customer=Customer::where('customer_id',$item->customer_id)->first();
            if (!$customer){
                continue;
            }
            $project_id = 60;
            /**
             * @var Customer $customer
             */
            $lead = new Lead();
            $lead->create_user_id = 1;
            $project= new  ProjectUser();
            $lead->owner_user_id = $project->current($project_id);
            $lead->next_time = 0;
            $lead->next_time = 0;
            $lead->customer_id = 0;
            $lead->source = $customer->source;
            $lead->is_transform = 0;
            $lead->name = $customer->name;
            $lead->mobile = $customer->mobile;
            $lead->create_time = time();
            $lead->is_send=1;
            $lead->status = '潜在客户';
            $lead->project_id = $project_id;
            $lead->update_time = time();
            $lead->save();
          /*  $sudoLeads = Lead::where('mobile', $lead->mobile)->where(
                'leads_id', '!=', $lead->leads_id
            )->get();
            if ($sudoLeads->isNotEmpty()) {
                foreach ($sudoLeads as $sudoLead) {
                    if ($sudoLead->customer_id != 0) {
                        $records = Record::where('types', 'crm_customer')
                            ->where('types_id', $sudoLead->customer_id)
                            ->get();
                        $this->setRecord($records, $sudoLead, $lead);
                        $this->setRecordCustomer($records, $sudoLead, $lead);
                    }
                    $records = Record::where('types', 'crm_leads')
                        ->where('types_id', $sudoLead->leads_id)
                        ->get();
                    $this->setRecord($records, $sudoLead, $lead);
                    $this->setRecordCustomer($records, $sudoLead, $lead);
                }
            }*/
            if (config('app.env') === 'production') {
                try {

                    $user = User::find($lead->owner_user_id);
                    if ($user) {
                        $user->notify(new \App\Notifications\LeadsNotification($lead));
                    }

                } catch (\Exception $exception) {

                }
            }
            $item->is_send = 1;

            $item->save();
        }
    }


    public function setRecord($records, $sudoLead, $lead)
    {
        if ($records->isNotEmpty()) {
            foreach ($records as $record) {
                $record2 = Record::where('types_id', $lead->leads_id)
                    ->where('types', 'crm_leads')
                    ->where('create_time', $record->create_time)
                    ->first();
                if ($record2) {
                    $this->info('存在');
                    continue;
                }
                $record1 = new Record();
                $record1->types = 'crm_leads';
                $record1->types_id = $lead->leads_id;
                $record1->content = $record->content;
                $record1->category = $record->category;
                $record1->next_time = $record->next_time;
                $record1->business_ids = $record->business_ids;
                $record1->create_time = $record->create_time;
                $record1->update_time = $record->update_time;
                $record1->create_user_id = $record->create_user_id;
                $record1->status = $record->status;
                $record1->project_id = $record->project_id;
                $record1->save();
                //  $this->info($record1->record_id);

            }
        }
    }

    public function setRecordCustomer($records, $sudoLead, $lead)
    {
        if ($lead->customer_id != 0) {
            if ($records->isNotEmpty()) {
                foreach ($records as $record) {
                    $record2 = Record::where('types_id', $lead->customer_id)
                        ->where('create_time', $record->create_time)
                        ->first();
                    if ($record2) {
                        continue;
                    }
                    $record1 = new Record();
                    $record1->types = 'crm_customer';
                    $record1->types_id = $lead->customer_id;
                    $record1->content = $record->content;
                    $record1->category = $record->category;
                    $record1->next_time = $record->next_time;
                    $record1->business_ids = $record->business_ids;
                    $record1->create_time = $record->create_time;
                    $record1->update_time = $record->update_time;
                    $record1->create_user_id = $record->create_user_id;
                    $record1->status = $record->status;
                    $record1->project_id = $record->project_id;
                    $record1->save();

                    //  $this->info($record1->record_id . 'customer');
                }
            }
        }
    }

}
